<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id')->unsigned()->comment('文章id');
            $table->string('article_picture', 255)->nullable()->comment('文章展示图片');
            $table->string('subject', 100)->nullable()->comment('文章标题');
            $table->string('sec_subject', 100)->nullable()->comment('副标题');
            $table->text('content')->nullable()->comment('文章详情，富文本');
            $table->dateTime('create_time')->nullable()->comment('创建时间');
            $table->integer('view_number')->default(0)->unsigned()->nullable()->comment('浏览量');
            $table->integer('sort_order')->unsigned()->nullable()->comment('排序号');
            $table->integer('cate_id')->unsigned()->comment('新闻分类ID');
            $table->foreign('cate_id')->references('id')->on('article_cates');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles');
    }

    public function getFullPicturePath()
    {
        if (!empty($this->article_picture)) {
            return FastDFS::getFullUrl($this->article_picture);
        }
        return '/images/no-pic-back.png';
    }

    public function articleCate()
    {
        return $this->belongsTo('App\ArticleCates', 'cate_id', 'id');
    }
}
